<!DOCTYPE html>
<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="/res/back/css/base.css" rel="stylesheet" type="text/css"/>
    <link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
    <script src="/res/third/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
    <script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
    <script src="/res/common/js/util.js" type="text/javascript"></script>

    <style>
        .noselect {
            background-color: rgba(99, 230, 99, 0.8);
        }
    </style>

</head>

<body>
[#if contents ?? && maxlength!=0 ]
<div class="panel panel-default panel-content" style="overflow: auto;">
    <div class="panel-heading" style="position: fixed;height: 70px;width: 99%;">
        <table>
            <tr>
                公司名称: ${(quote.companyName)!}, 询价名称:${(enquiry.enquiryName)!}
            </tr>
            <tr>
                <td class="w80 ">
                    <button type="button" class="btn  btn-success" onclick="submodel()">确定</button>
                </td>

                <td class="w80 ">
                    <button type="button" class="btn  btn-info" onclick="shownocontent()">查看未匹配行</button>
                </td>
                <td class="w80 ">
                    <button id="mulityset" type="button" class="btn  btn-primary " style="display: none;" onclick="mulityset()">批量设置系统</button>
                </td>
                <td  class="w80 ">
                    <button  id="delLine"  type="button" class="btn  btn-danger" style="display: none;"   onclick="delLine()" >删除行</button>
                </td>
                <td>
                    [#if titlePerfect]&nbsp;&nbsp;<b id="icon_140" class="glyphicon glyphicon-thumbs-up"></b>[/#if]
                    [#if contentPerfect ]&nbsp;&nbsp;<b id="icon_140" class="glyphicon glyphicon-thumbs-up">&nbsp;&nbsp;&nbsp;&nbsp;</b>[/#if]
                </td>

            </tr>

        </table>
    </div>
    <form id="itemform" method="post" style="padding-top: 70px;">
        <input type="hidden" value="${quote.id}" name="quoteid" id="quoteid" />
        <div>
            <table id="itemTable"  class="table table-hover table-bordered table-striped table-condensed">
                <thead>
                <tr>
                    <th>#</th>
                    <th>系统名称</th>
                    [#assign  index = maxlength /]

                    [#list 1..index  as b]
                        <th class="cell_${b}">
                            <select class="typename" name="typename" attnum=${b}>

                                [#if titleindex?? && (titleindex?size > b-1) ]
                                    [#assign a = titleindex[b-1] ]
                                [#else]
                                    [#assign a = 0 ]
                                [/#if ]

                                <option value="0">请选择</option>
                                <option  [#if a==1] selected="selected" [/#if] value="1">设备名称</option>
                                <option  [#if a==2] selected="selected" [/#if] value="2">品牌</option>
                                <option  [#if a==3] selected="selected" [/#if] value="3">型号</option>
                                <option  [#if a==4] selected="selected" [/#if] value="4">单位</option>
                                <option  [#if a==5] selected="selected" [/#if] value="5">数量</option>
                                <option  [#if a==6] selected="selected" [/#if] value="6">技术参数</option>
                                <option  [#if a==7] selected="selected" [/#if] value="7">单价</option>
                            </select>

                        </th>
                    [/#list]
                    <th >总价</th>
                </tr>
                <tr>
                    <td>
					<span>
						全<input type="checkbox" class="checkedall">
					</span>
                    </td>
                    <td>
                        <span style="font-size: 10px;color: #918C8C;">原始名称 ：</span>
                    </td>
                    [#list 1..index  as a]
                        <td class="cell_${a}">
                            <button onclick="delcell(${a})" class="btn btn-xs btn-danger">删除该列</button>
                        [#--原始列名--]
                            [#if titles?? && (titles?size > a-1) ]
                                <span style="font-size: 10px;color: #918C8C;">${titles[a-1]}</span> <input type="hidden" name="oldtitle" value="${titles[a-1]}"/>
                            [/#if ]
                        </td>
                    [/#list]

                    <td id="totalprice"></td>
                </tr>
                </thead>
                [#assign  index = 0 /]
                [#list contents as cc ]
                    <tr attindex="${index+1}" >
                        <td>
                            <label> <span>${index+1}</span> <span>
								<input type="checkbox" value="${index}" class="checksystem"/>
							</span> </label> <input type="hidden" name=".quoteId" value="${quote.id!}"/>
                        </td>

                        [#list cc as c]
                            [#if c_index==0]
                                <td>
                                [#--md5值是用来区别: 有多个系统被--]
                                        <input id="system_${index}" type="button"   attshebei=[#if  md5secret?? && md5secret?size!=0 && shebeiindex!=-1 ] "${md5secret[index]}" [#else]""[/#if]
                                    value="[#if cc[0]=="" ]选择系统[#elseif cc[0]?index_of(",")!=-1 ]有多个匹配[#else]${categorysMap[cc[0]].name!}[/#if]" class="w120 btn [#if cc[0]=="" ]
                                    btn-default[#elseif cc[0]?index_of(",")!=-1  ]btn-danger[#else]btn-success[/#if]" onclick="chooseService(this,${index+1})" />
                                    <input class="fcateid" type="hidden" name=".categoryPId" value="[#if cc[0]!="" ]${cc[0]}[/#if]"/>
                                    <input type="hidden" name=".categorySId" value="" />
                                [#--无法识别的系统，需要保存到数据库--] <input type="hidden" name=".needrepository"   value=[#if cc[0]==""]1[#else]0[/#if]  >
                                </td>
                            [#else]
                                <td class="cell_${c_index}"><input name="" class="input_${c_index}" type="text" value="${c!?html}"></td>
                            [/#if]
                        [/#list]
                        [#if cc?size-1 < maxlength]
                        [#--补充空白列--]
                            [#list 0..(maxlength - cc?size) as c]
                                <td class="cell_${c_index + cc?size}"><input name="" class="input_${c_index+ cc?size}" type="text" value=""></td>
                            [/#list]
                        [/#if]

                        <td class="totalprice">1</td>
                    </tr>
                    [#assign  index = index+1 /]
                [/#list]

            </table>
        </div>
    </form>
</div>
<div style="display: none;">
    <div id="nocontent">
        <div style="overflow: scroll;max-height: 500px;min-width:300px;">
            [#if nocontents?? ]
                <table>
                    [#list nocontents  as nocontent]
                        <tr>
                            [#list nocontent  as c]
                                [#if c!=""]
                                    <td style="border-bottom: 1px solid black;">&nbsp;${c!}&nbsp;</td>
                                [/#if]
                            [/#list]</tr>
                    [/#list]</table>
            [#else]
                没有
            [/#if]</div>
    </div>

</div>

<script type="text/javascript">
    //设备名称，品牌 ，型号  ,单位 ,数量,技术参数
    var selectnames = ["productName", "brand", "model", "unit", "quantity", "descr", "unitprice"];
    //切换选项，确保不重复就可
    $(".typename").on("change", function () {

        var thisval = $(this).val();

        if (thisval != 0) {//将相同的选项设置为请选择
            var hadvnum = [];
            hadvnum.push(thisval);

            $(".typename").each(function (i) {
                var otherv = $(this).val();
                if (otherv == thisval) {
                    $(this).val(0);
                } else {
                    hadvnum.push(otherv);
                }
            });

            //没有被选择的加颜色
            var nums = hadvnum.join("");
            $(".noselect").removeClass("noselect");
            for (var i = 1; i < 7; i++) {
                if (nums.indexOf(i) == -1) {
                    $(".typename").find("option[value='" + i + "']").addClass("noselect");
                }
            }

        }

        $(this).val(thisval);

        //重新设置name值
        everyInputName();
        //重新计算价格
        totalPrice(true);
    });


    //计算总价
    function totalPrice(refresh) {
        //打到数量与单价列,才能计算价格
        var hasnum = 0;
        var hasprice = 0;
        var success = true;
        $(".typename").each(function (i) {
            var otherv = $(this).val();
            if (otherv == 5) {//数量
                hasnum = i + 1;
            } else if (otherv == 7) {//单价
                hasprice = i + 1;
            }
        });
        if (hasnum != 0 && hasprice != 0) {
            var alltotal = 0;
            //计算总价
            $(".input_" + hasnum).each(function (i) {
                var num = $(this).val();
                var priceobj = $(".input_" + hasprice).get(i);
                var price = $(priceobj).val();

                if (num == "" || isNaN(num) || parseFloat(num) < 0) {
                    success = false;
                    return false;
                }

                if (price == "" || isNaN(price) || parseFloat(price) < 0) {
                    success = false;
                    return false;
                }

                var t = mul(parseFloat(num), parseFloat(price)).toFixed(2);


                $($(".totalprice").get(i)).text(t);
                alltotal = add(alltotal, t).toFixed(2);
            });
            $("#totalprice").text(alltotal);
        } else {
            success = false;
        }

        if (!success) {
            $(".totalprice").each(function () {
                $(this).text("");
            })
            $("#totalprice").text("")
        }

    }

    function everyInputName() {
        //确定每列的name
        $(".typename").each(function (i) {
            var thisval = $(this).val();
            var attnum = $(this).attr("attnum");
            //拿到name值
            var namev = selectnames[thisval - 1];
            //设置name值
            $(".input_" + attnum).each(function () {
                $(this).attr("name","."+namev);
            })
        });
    }

    //初始化
    (function () {
        //计算总价
        totalPrice(true);
        //第个Input设置name值
        everyInputName();
        //数量价格绑定keyup事件
        $("body").on("keyup", "input[name$='quantity'],input[name$='unitprice']", function () {
            totalPrice(true);
        })
    })();

    //准备提交
    function submodel() {
        var success = true;
        var msg = "";
        $(".typename").each(function () {
            var thisval = $(this).val();
            if (thisval == 0) {
                success = false;
                msg = "失败：请确定每一列的名称";
                return false;
            }

        });

        if (!success) {
            art.dialog.tips(msg);
            return;
        } else {
            everyInputName();

            //验证数值 ,数量必须全为数字
            $("#itemform").find("input[name$='quantity']").each(function () {
                var num = $(this).val();
                if (num == "" || isNaN(num) || parseFloat(num) < 0) {
                    success = false;
                    return false;
                }
            });

            if (!success) {
                art.dialog.tips("数量对应的列必须全为数字且不能小于0!");
                return;
            }


            //验证价格,价格全部为数字
            $("#itemform").find("input[name$='unitprice']").each(function () {
                var pirce = $(this).val();
                if (pirce == "" || isNaN(pirce) || parseFloat(pirce) < 0) {
                    success = false;
                    return false;
                }
            });

            if (!success) {
                art.dialog.tips("单价对应的列必须全为数字且不能小于0!");
                return;
            }


            //验证设备名称
            $("#itemform").find("input[name$='productName']").each(function () {
                var productname = $(this).val();
                if (productname == "") {
                    success = false;
                    return false;
                }
            });

            if (!success) {
                art.dialog.tips("设备名称不能有空！");
                return;
            }


            //单位不能为空
            $("#itemform").find("input[name$='unit']").each(function () {
                var unit = $(this).val();
                if (unit == "") {
                    success = false;
                    return;
                }
            });

            if (!success) {
                art.dialog.tips("单位不能有空！");
                return;
            }


            $(".fcateid").each(function () {
                var v = $(this).val();
                if (v == "" || v.indexOf(",") != -1) {
                    success = false;
                    return false;
                }
            });
            if (!success) {
                art.dialog.tips("必须选择系统！");
                return;
            }


            var pricesize = $("#itemform").find("input[name$='unitprice']").size();
            if (pricesize == 0) {
                success = false;
                art.dialog.tips("缺少单价列,excel不规范");
                return;
            }

            var quantitysize = $("#itemform").find("input[name$='quantity']").size();
            if (quantitysize == 0) {
                success = false;
                art.dialog.tips("缺少数量列,excel不规范");
                return;
            }

            var productNameSize = $("#itemform").find("input[name$='productName']").size();
            if (productNameSize == 0) {
                success = false;
                art.dialog.tips("缺少设备名称列,excel不规范");
                return;
            }


            var items =[];
            var typenames =[];
            var oldtitles=[];
            $("#itemTable").find("tr").each(function () {
                var obj={};
                $(this).find("input[name]").each(function () {
                    var names = $(this).attr("name");
                    var inputval = $(this).val();
                    if(names.indexOf(".")!=-1){
                        var k = names.substring(1);
                        obj[k]=inputval;
                    }else{
                        if(names=="oldtitle"){
                            oldtitles.push(inputval);
                        }
                    }
                });
                if(!jQuery.isEmptyObject(obj)){
                    items.push(obj);
                }
            });

            $("select[name='typename']").each(function(){
                typenames.push($(this).val());
            });

            var obj={item:items,typenames:typenames,oldtitles:oldtitles,quoteid:$("#quoteid").val()};

            //设置后提交
            art.dialog.post({
                url: "/enq/o_save_quote_item.do",
                data:JSON.stringify(obj),
                contentType:"application/json",
                success: function (data) {
                    if (data.success) {
                        //refresh();
                        //更新原
                        art.dialog.alert("操作成功", function () {
                            top.closeTempTab(true);
                        })
                    } else {
                        art.dialog.tips(data.msg);
                    }
                }
            });

        }
    }

    function delcell(cellnum) {
        $(".cell_" + cellnum).remove();
    }


    var cobj;
    var cdialog;
    var ismulity = false;
    //选择器
    function chooseService(obj, index) {
        ismulity = false;

        cobj = $(obj);
        if (cdialog) {
            cdialog.close();
        }
        var ccv = cobj.next().val();
        var attname = cobj.attr("attshebei");
        cdialog = art.dialog.load("选择系统->序号:" + index, "/category/v_choose_category.do?curent=" + ccv + "&attname=" + attname);
    }

    //选择回调
    function chooseCallback(name, id, topid) {
        if (!ismulity) {
            if (name && id) {
                cobj.val(name);//显示名称
                cobj.removeClass("btn-danger").addClass("btn-success");
                cobj.next().val(id).next().val(topid).next().val(1);//显示值
            }
        } else {
            if (name && id) {
                $(".checksystem:checked").each(function () {
                    var checkedval = $(this).val();
                    $("#system_" + checkedval).val(name).addClass("btn-success").next().val(id).next().val(topid).next().val(1);
                    $(this).attr("checked", false);
                });
                $(".checkedall")[0].checked = false;
            }
        }
        cdialog.close();
    }

    $(".checksystem").each(function () {
        $(this).click(function () {
            var checkedsize = $(".checksystem:checked").size();
            if (checkedsize == 0) {
                $("#mulityset").hide();
                $("#delLine").hide();
            } else {
                $("#mulityset").show();
                $("#delLine").show();
            }
        });
    })
    function delLine() {
        art.dialog.confirm("确认删除",function(){
            var _this = null;
            var indexnum=null;
            $(".checksystem:checked").each(function(i){
                var trobj = $(this).parents("tr");
                if(i==0){
                    _this = $(this);
                }else{
                    trobj.remove();
                }
            });

            var _parent = _this.parents("tr");
            var indexnum = parseInt(_parent.attr("attindex"));
            _parent.nextAll().each(function(){
                $(this).attr("attindex",indexnum);
                $(this).find("td").eq(0).find("span").eq(0).text(indexnum);
                indexnum++;
            });
            _parent.remove();

            $("#mulityset").hide();
            $("#delLine").hide();
            totalPrice(true);
            art.dialog.tips("已删除");
        });

    }

    //批量设置系统
    function mulityset() {
        ismulity = true;
        if (cdialog) {
            cdialog.close();
        }
        cdialog = art.dialog.load("选择系统->批量设置", "/category/v_choose_category.do");
    }

    //全选方法
    $(".checkedall").click(function () {
        var setchecked = false;
        if ($(this).is(":checked")) {
            setchecked = true;
            $("#mulityset").show();
            $("#delLine").show();
        } else {
            $("#mulityset").hide();
            $("#delLine").hide();
        }

        $(".checksystem").each(function () {
            $(this)[0].checked = setchecked;
        })
    });

    //展示未匹配的
    function shownocontent() {
        art.dialog({
            title: "未匹配",
            content: document.getElementById('nocontent')
        });
    }

</script>

[#else]
${msg!"没有任何信息"}
[/#if]

</body>
</html>